package cz.tpj.database.aktivity;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

import cz.tpj.database.DatabaseConnector;
import cz.tpj.database.akcia.Table_Akcia;
import cz.tpj.util.ErrorDatabase;

/**
 * Trieda sprostredkuvava metody pre vyberanie
 * Aktivit z databazy
 * @author jabo
 */

public class Table_Aktivita {

    public static final String TABLE       = "aktivity";
    public static final String ID          = "id";
    public static final String NAME        = "nazov";

    
    private static Table_Aktivita im = null;
    private Connection con;
    private Statement stmt = null;

    public static Table_Aktivita getInstance(){
        if(im == null){
            im = new Table_Aktivita();
        }
        return im;
    }
    //=======================metody-------------------------
    private void databaseConnect() throws SQLException{
        DatabaseConnector dc = DatabaseConnector.getInstance();
        con = dc.getConnection();
        stmt = con.createStatement();
    }
	
    /**
     * vrati vsetky aktivity
     * @return ArrayList<Aktivita>
     */
    public ArrayList<Aktivita> getAktivity() throws ErrorDatabase {
        ResultSet rs = null;
        ArrayList<Aktivita> score = new ArrayList<Aktivita>();
        try {
            databaseConnect();
            rs = stmt.executeQuery("SELECT * FROM " + TABLE + ";");
            while(rs.next()){
                String id = rs.getString(ID);
                String nazov = rs.getString(NAME);

                Aktivita temp = new Aktivita(id, nazov);
                score.add(temp);
            }
        } catch (SQLException ex) {
            Logger.getLogger(Table_Akcia.class.getName()).log(Level.SEVERE, null, ex);
        } finally{
            try {
                rs.close();
                stmt.close();
            } catch (Exception ex) {
            }

        }
        return score;
    }
	
}
